﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Objects.DataClasses;
using System.Data.Services.Client;
using System.Data.Services.Common;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Xml.Linq;
using ZhiHeng.OneCard.WpfClient.Common;
using System.Windows;

namespace System.Data.Services.Common
{


    ///// <summary>
    ///// Properties marked with this Attribute are not serialized in the payload when sent to the server
    ///// </summary>
    //[AttributeUsage(AttributeTargets.Property)]
    //public class DoNotSerializeAttribute : Attribute
    //{
    //}
}


namespace ZhiHeng.Consume.DataContract
{
    /*
    partial class OneCardParkEntities
    {
        public OneCardParkEntities(Uri dataServiceUri, bool enableIgnoreProperties = true)
            : this(dataServiceUri)
        {
            this.WritingEntity += new EventHandler<ReadingWritingEntityEventArgs>(DataServiceContextEx_WritingEntity);
        }

        void DataServiceContextEx_WritingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            // e.Data gives you the XElement for the Serialization of the Entity 
            //Using XLinq  , you can  add/Remove properties to the element Payload  
            XName xnEntityProperties = XName.Get("properties", e.Data.GetNamespaceOfPrefix("m").NamespaceName);
            XElement xePayload = null;
            foreach (PropertyInfo property in e.Entity.GetType().GetProperties())
            {
                object[] doNotSerializeAttributes = property.GetCustomAttributes(typeof(DoNotSerializeAttribute),
                                                                                 false);
                if (doNotSerializeAttributes.Length > 0)
                {
                    if (xePayload == null)
                    {
                        xePayload =
                            e.Data.Descendants().Where<XElement>(xe => xe.Name == xnEntityProperties).First<XElement>();
                    }
                    //The XName of the property we are going to remove from the payload
                    XName xnProperty = XName.Get(property.Name, e.Data.GetNamespaceOfPrefix("d").NamespaceName);
                    //Get the Property of the entity  you don't want sent to the server
                    foreach (XElement xeRemoveThisProperty in xePayload.Descendants(xnProperty).ToList())
                    {
                        //Remove this property from the Payload sent to the server 
                        xeRemoveThisProperty.Remove();
                    }
                }
            }
        }

    }
    */
      


}
